<template>
  <div>
    <h1>仪器设备</h1>
    <el-alert
      v-if="del_success===1"
      title="删除成功"
      type="success">
    </el-alert>
    <div>
      <el-row :gutter="20">
        <el-col :span="18"><div>
          <el-radio-group v-model="radio1">
            <el-radio-button label="仪器编号"></el-radio-button>
            <el-radio-button label="机构编号"></el-radio-button>
          </el-radio-group>
        </div></el-col>
        <el-col :span="2"><div>
          <el-button type="text" @click="this.addInstrument">添加仪器信息</el-button>
        </div></el-col>
        <el-col :span="2"><div>
          <el-button type="text" @click="this.selectinfo">清空检索</el-button>
        </div></el-col>
      </el-row>
    </div>
    <el-input v-model="userinfo" placeholder="请输入信息"></el-input>
    <el-button type="primary"
               style="width: 100%;margin-top: 20px; margin-bottom: 30px; border-radius:0px; box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)"
               class="el-icon-search" @click="select">检索</el-button>
    <el-table
      :data="info1"
      height="360"
      border
      style="width: 100%">
      <el-table-column
        prop="yid"
        label="仪器编号"
        align="center"
        width="120"
        :resizable="false">
      </el-table-column>
      <el-table-column
        prop="jid"
        label="机构编号"
        align="center"
        width="120"
        :resizable="false">
      </el-table-column>
      <el-table-column
        prop="ytext"
        label="仪器名称"
        align="center"
        width="220"
        :resizable="false">
      </el-table-column>
      <el-table-column
        prop="insertdate"
        label="仪器添加时间"
        align="center"
        width="220"
        :resizable="false">
      </el-table-column>
      <el-table-column
        prop="state"
        label="仪器状态"
        align="center"
        width="180"
        :resizable="false">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="210"
        :resizable="false">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle @click="edit(scope.row.yid)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="del(scope.row.yid)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: "IdentifyList" ,
  data(){
    return {
      id: '',
      radio1: '仪器编号',
      userinfo: '',
      del_success:'',
      info1:[],
      info:{
        jid:'1'
      }
    }
  },
  beforeRouteEnter: (to, from, next) => {
    console.log('进入路由之前');//加载数据
    next(vm => {
      vm.selectinfo();
    })
  },
  beforeRouteLeave: (to, from, next) => {
    console.log('进入路由之后');
    next()
  },
  methods:{
    addInstrument:function (){
      this.$router.push('/addInstrument');
    },
    selectinfo:function (){
      this.userinfo='';
      this.info1=[];
    },
    select:function (){
      if(this.userinfo===''){
        this.$alert('请输入查询信息', '错误提示', {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
      }
      else{
        if(this.radio1==='仪器编号'){
          this.axios({
            method:"post",
            headers: { 'content-type': 'application/json' },
            url: "http://evidence.frogking.cn/instrumentAdm/selectByYid",
            params:{
              token:localStorage.getItem("token_only"),
              yid:this.userinfo
            }
          }).then(res=>{
            console.log(res)
            if(res["data"]["code"]==='706'){
              this.info1=[res["data"]["item"]];
            }
            else if(res["data"]["code"]==='708'){
              this.$alert('该仪器不存在', '错误提示', {
                confirmButtonText: '确定',
                callback: action => {
                  this.userinfo="";
                }
              });
            }
            else if(res["data"]["code"]==='000'){
              this.$alert('token失效', '错误提示', {
                confirmButtonText: '确定',
                callback: action => {
                  this.$router.push("/login");
                  localStorage.removeItem("token_only");
                }
              });
            }
            else if(res["data"]["code"]==='001'){
              this.$alert('身份不满足，禁止操作', '错误提示', {
                confirmButtonText: '确定'
              });
            }
            else if(res["data"]["code"]==='002'){
              this.$alert('权限不满足，禁止操作', '错误提示', {
                confirmButtonText: '确定'
              });
            }
          }).catch(function (error) {
            console.log(error);
          });
        }
        else if(this.radio1==='机构编号'){
          this.axios({
            method:"post",
            headers: { 'content-type': 'application/json' },
            url: "http://evidence.frogking.cn/instrumentAdm/selectByJid",
            params:{
              token:localStorage.getItem("token_only"),
              jid:this.userinfo
            }
          }).then(res=>{
            console.log(res)
            if(res["data"]["code"]==='706'){
              this.info1=res["data"]["item"];
              console.log(this.info1)
            }
            else if(res["data"]["code"]==='708'){
              this.$alert('仪器不存在', '错误提示', {
                confirmButtonText: '确定',
                callback: action => {
                  this.userinfo="";
                }
              });
            }
            else if(res["data"]["code"]==='000'){
              this.$alert('token失效', '错误提示', {
                confirmButtonText: '确定',
                callback: action => {
                  this.$router.push("/login");
                  localStorage.removeItem("token_only");
                }
              });
            }
            else if(res["data"]["code"]==='001'){
              this.$alert('身份不满足，禁止操作', '错误提示', {
                confirmButtonText: '确定'
              });
            }
            else if(res["data"]["code"]==='002'){
              this.$alert('权限不满足，禁止操作', '错误提示', {
                confirmButtonText: '确定'
              });
            }
          }).catch(function (error) {
            console.log(error);
          });
        }
      }
    },
    edit:function (data){
      this.$router.push('/editInstrument'+'/'+data);
    },
    del:function (op_id){
      this.axios({
        method:"post",
        headers: { 'content-type': 'application/json' },
        url: "http://evidence.frogking.cn/instrumentAdm/delete",
        params:{
          token:localStorage.getItem("token_only"),
          yid:op_id,
        }
      }).then(res=>{
        if(res["data"]["code"]==='702'){
          this.selectinfo();
          this.del_success=1;
        }
        else if(res["data"]["code"]==='703'){
          this.$alert('系统错误！删除异常！', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.userinfo="";
            }
          });
        }
        else if(res["data"]["code"]==='000'){
          this.$alert('token失效', '错误提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push("/login");
              localStorage.removeItem("token_only");
            }
          });
        }
        else if(res["data"]["code"]==='001'){
          this.$alert('权限不满足，禁止操作', '错误提示', {
            confirmButtonText: '确定'
          });
        }
        else if(res["data"]["code"]==='002'){
          this.$alert('权限不满足，禁止操作', '错误提示', {
            confirmButtonText: '确定'
          });
        }
      }).catch(function (error) {
        console.log(error);
      });
    }
  }
}
</script>

<style scoped>
</style>
